/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.screenmanagement;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.dnd.DropTarget;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import java.awt.Container;

import ontocompo.ui.merge.PlugComponent;
import ontocompo.ui.positioning.DragManager;
import ontocompo.ui.positioning.GridRelativePane;

public class Screen /*extends JPanel*/ {
	
	private String name;
	
	private ArrayList<Component> inBulk;
	private GridRelativePane rootScreen;
	
	private HashMap<JComponent, Screen> screenFlow;
	private ArrayList<PlugComponent> pluggedComponents;
	
	private DragManager dm;
	
	private boolean haveANextScreenButton;
	
	public Screen(String name) {
		super();
		
		this.name = name;
//		this.setBackground(Color.WHITE);
		
		this.inBulk = new ArrayList<Component>();
		
		this.screenFlow = new HashMap<JComponent, Screen>();
		
		this.pluggedComponents = new ArrayList<PlugComponent>();
		
		this.haveANextScreenButton = false;
//		this.setLayout(new GridBagLayout());
		
//		this.addMouseListener(new DragManager());
	}
	
	public String getName() {
		return this.name;
	}
	
	public void addInBulk(Component jc) {
		this.inBulk.add(jc);
	}
	
	public ArrayList<Component> getInBulkComponents() {
		return this.inBulk;
	}
	
	public GridRelativePane getRootScreen() {
		return this.rootScreen;
	}
	
	public void setRootScreen(GridRelativePane grp) {
		this.rootScreen = grp;
	}
	
	public void setDragManager(DragManager dm) {
		this.dm = dm;
	}
	
	public void removeFromBulkList(Component c) {
		
		for(Component comp :  this.inBulk) {
			
			if(JPanel.class.getClass().isInstance(comp.getClass())) {
				Container cont = (Container) comp;
				
				boolean removeOk = false;
				
				removeOk = this.removeFromBulkList(cont, c);
				
				if(removeOk) {
					if(cont.getComponents().length == 0) {
						this.inBulk.remove(comp);
						break;
					}
				}
			} else {
				if(comp.equals(c)) {
					this.inBulk.remove(c);
					break;
				}
			}
		}
		
		
	}
	
	private boolean removeFromBulkList(Container cont, Component c) {
		
		
		boolean removeOk = false;
		
		for(Component comp : cont.getComponents()) {
			if(comp.getClass().isAssignableFrom(Container.class)) {
				Container cont2 = (Container) comp;
				removeOk = this.removeFromBulkList(cont2, c);
				
				if(removeOk) {
					if(cont2.getComponents().length == 0) {
						cont.remove(cont2);
						break;
					}
				}
			} else {
				if(comp.equals(c)) {
					cont.remove(c);
					removeOk = true;
					break;
				}
			}
		}
		
		return removeOk;
	}
	
/*	public void removeComponentFromBulk(JComponent toRemove) {
		this.inBulk.remove(toRemove);
	}
	
	public void moveJComponent(JComponent toMove, DropTarget dt) {
		this.inBulk.remove(toMove);
		if(this.rootScreen == null) {
			this.rootScreen = new GridRelativePane(toMove, dt);
		}
		
	}
*/
	public HashMap<JComponent, Screen> getScreenFlow() {
		return this.screenFlow;
	}
	
	public void addToScreenFlow(JComponent jc, Screen s) {
		this.screenFlow.put(jc, s);
	}
	
	public JButton addNextScreenButton() {
		if(! this.haveANextScreenButton) {
			if(this.rootScreen.getBottomright() != null) {
				GridRelativePane newRootScreen = new GridRelativePane(this.rootScreen);
				this.setRootScreen(newRootScreen);
			}
			
			JButton nextScreenButton = new JButton("Next Screen");
			
			this.rootScreen.addOnBottomRight(nextScreenButton);
			
			this.rootScreen.drying(true);
			
			this.rootScreen.validate();
			
			this.haveANextScreenButton = true;
			
			return nextScreenButton;
		} else {
			return null;
		}
	}
	
	public boolean hasANextScreenButton() {
		return this.haveANextScreenButton;
	}
	
	public ArrayList<PlugComponent> getPluggedComponents() {
		return this.pluggedComponents;
	}
	
	public void addInPluggedComponents(PlugComponent pc) {
		this.pluggedComponents.add(pc);
	}
	
	public void removeFromPluggedComponents(PlugComponent pc) {
		this.pluggedComponents.remove(pc);
	}
	
	public String toString() {
		return "Screen '" + this.name + "'";
	}
}
